<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\model;

use app\api\model\user\BalanceLog as BalanceLogModel;
use app\api\service\User as UserService;
use app\api\model\Activity as Activity;
use app\common\enum\user\balanceLog\Scene as SceneEnum;
use app\common\model\ActivityLog as ActivityLogModel;
use cores\exception\BaseException;
use app\api\service\Activity\RegActivity;

/**
 * 用户参加活动日志
 * Class ActivityLog
 * @package app\api\model
 */
class ActivityLog extends ActivityLogModel
{

    /**
     * 参加活动
     * @param int $activityId 活动ID
     * @return bool
     * @throws BaseException
     */
    public function receive(int $activityId): bool
    {
        // 当前用户ID
        $userId = UserService::getCurrentLoginUserId(true);
        // 获取活动信息
        $activityInfo = Activity::detail($activityId);
        if (empty($activityInfo)) {
            $this->error = '当前活动不存在';
            return false;
        }

        if (($activityInfo['end_time'] + 86400) < time()) {
            $this->error = '活动已过期';
            return false;
        }
        // 验证优惠券是否可领取
        if ($this->checkReceive($userId, $activityId)) {
            $this->error = '现金券已领取,不能重复领取';
            return false;
        }
        $activity_type  = 6;
        $activity_value = 50;
        $remarks        = '新用户赠送50元现金券';
        $result         = RegActivity::sendByUserIdCoupon($userId, $activity_type, $activity_value, $remarks);
        if ($result['state'] && $result['data']) {
            $data = [
                'activity_method' => 1,
                'activity_type'   => $activity_type,
                'activity_value'  => $activity_value,
                'create_time'     => time(),
                'remarks'         => $remarks,
                'proof'           => $result['data']
            ];
           return $this->transaction(function () use ($userId, $activityId, $data) {
                // 添加领取记录
                $this->add($userId, $activityId, $data);
                // 新增余额变动记录
                BalanceLogModel::add(SceneEnum::REGACTIVITY, [
                    'user_id'         => (int)$userId,
                    'money'           => 0.00,
                    'correlation_id'  => $this->id,
                    'bd_type'         => $data['activity_type'],
                    'deduction_value' => $data['activity_value'],
                ], ['order_no' => $data['remarks']]);
                return true;
            });
        }
        return false;
    }
}
